﻿using lxwmvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web.Mail;

namespace velocity_weixin
{
    public class EMailCore
    {
        /// <summary>
        /// 对方显示发送方的电子邮件
        /// </summary>
        private static string fromAddress { get; set; }
        /// <summary>
        /// 电子邮件用户名
        /// </summary>
        private static string userName { get; set; }
        /// <summary>
        /// 电子邮件密码
        /// </summary>
        private static string userPwd { get; set; }
        /// <summary>
        /// 电子邮件服务器
        /// </summary>
        private static string mailHost { get; set; }



        /// <summary>
        /// 发送电子邮件 SMTP方式
        /// </summary>
        /// <param name="mailAddress">你要发送的邮箱地址</param>
        /// <param name="mailTitle">邮件主题</param>
        /// <param name="mailContent">邮件的内容</param>
        public static void SendMail(string mailAddress, string mailTitle, string mailContent)
        {
            fromAddress = "提醒<gaolianhuanyu@163.com>";
            userName = "gaolianhuanyu@163.com";
            userPwd = "7521gg";
            mailHost = "smtp.163.com";

            System.Net.Mail.MailMessage objMailMessage = new System.Net.Mail.MailMessage();
            //string fromAddress = ConfigurationManager.AppSettings["FromAddress"];//你在web.config中配置的发件人地址，就是你的邮箱地址。
            //string mailHost = ConfigurationManager.AppSettings["MailHost"];//邮件服务器，如mail.qq.com

            objMailMessage.From = new MailAddress(fromAddress);//发送方地址
            objMailMessage.To.Add(new MailAddress(mailAddress));//收信人地址
            objMailMessage.BodyEncoding = System.Text.Encoding.UTF8;//邮件编码
            objMailMessage.Subject = mailTitle;//邮件标题
            objMailMessage.Body = mailContent;//邮件内容
            objMailMessage.IsBodyHtml = true;//邮件正文是否为html格式

            SmtpClient objSmtpClient = new SmtpClient();
            objSmtpClient.Host = mailHost;//邮件服务器地址
            objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//通过网络发送到stmp邮件服务器
            objSmtpClient.Credentials = new System.Net.NetworkCredential(userName, userPwd);//发送方的邮件地址，密码
            //objSmtpClient.EnableSsl = true;//SMTP 服务器要求安全连接需要设置此属性
            //objSmtpClient.Port = 465;

            try
            {
                objSmtpClient.Send(objMailMessage);
            }
            catch (Exception ex)
            {
                dataloger.fatal(ex.Message, ex);//记录错误日志
            }
            finally
            {
                objMailMessage.Dispose();
            }
        }
    }
}